package com.zxq.ai.application;

import com.google.common.collect.Lists;
import com.zxq.common.utils.DateUtil;
import com.zxq.ai.application.gateway.WxGateway;
import com.zxq.ai.application.repository.CashAccountLogRepository;
import com.zxq.ai.application.repository.CashAccountRepository;
import com.zxq.ai.application.repository.RewardRedeemTaskRepository;
import com.zxq.ai.application.repository.UserRepository;
import com.zxq.ai.domain.constants.UserIdConstants;
import com.zxq.ai.domain.enums.RewardRedeemTaskStatus;
import com.zxq.ai.domain.reward.RewardRedeemTask;
import com.zxq.ai.domain.service.CashAccountDomain;
import com.zxq.ai.domain.user.User;
import com.zxq.ai.domain.user.account.CashAccount;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ScheduledTaskApplication {

    @Resource
    private RewardRedeemTaskRepository rewardRedeemTaskRepository;
    @Resource
    private CashAccountRepository cashAccountRepository;
    @Resource
    private CashAccountLogRepository cashAccountLogRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private CashAccountDomain cashAccountDomain;
    @Resource
    private WxGateway wxGateway;

    /**
     * 找到最早的1条发放
     */
    public void grantReward() {
        List<RewardRedeemTask> rewardRedeemTaskList = rewardRedeemTaskRepository.queryNotRedeemedTask(1);
        if (CollectionUtils.isEmpty(rewardRedeemTaskList)) {
            log.info("没有可发放的奖励: {}", DateUtil.ymdHms());
            return;
        }

        for (RewardRedeemTask redeemTask : rewardRedeemTaskList) {
            // 收款人
            Long payeeUserId = redeemTask.getPayeeUserId();
            // 付款人
            Long payerUserId = redeemTask.getPayerUserId();
            // 查询现金账户
            Map<Long, User> userId2UserMap = userRepository.queryByUserIdList(Lists.newArrayList(payerUserId, payeeUserId));
            Map<Long, CashAccount> userId2CashCountMap = cashAccountRepository.queryByUserIdList(Lists.newArrayList(payerUserId, payeeUserId));

            ///////////////////////////////////////////////////////////////////////////
            // 冻结账户资金
            cashAccountDomain.frozen4Transfer(userId2CashCountMap.get(payerUserId), userId2CashCountMap.get(payeeUserId), redeemTask.getRewardAmount(), UserIdConstants.SYSTEM_USER_ID);
            redeemTask.setStatus(RewardRedeemTaskStatus.DOING);
            log.info("冻结账户资金成功: {}", DateUtil.ymdHms());
            ///////////////////////////////////////////////////////////////////////////
            // 通过微信转账
            boolean success = wxGateway.transfer(userId2UserMap.get(payeeUserId), userId2UserMap.get(payerUserId), redeemTask.getRewardAmount());
            if (success) {
                log.info("通过微信转账成功: {}", DateUtil.ymdHms());
                cashAccountDomain.deliveryOfTransfer(userId2CashCountMap.get(payerUserId), userId2CashCountMap.get(payeeUserId), redeemTask.getRewardAmount(), UserIdConstants.SYSTEM_USER_ID);
                log.info("转账成功: {}", DateUtil.ymdHms());
                redeemTask.setStatus(RewardRedeemTaskStatus.SUCCESS);
                rewardRedeemTaskRepository.updateStatus(redeemTask);
            } else {
                log.info("通过微信转账失败: {}", DateUtil.ymdHms());
                return;
            }

        }
    }
}
